package com.limh.readers.widget.recyclerview.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import com.limh.readers.widget.recyclerview.base.ItemViewDelegate
import com.limh.readers.widget.recyclerview.base.ItemViewDelegateManager
import com.limh.readers.widget.recyclerview.base.ViewHolder


/**
 * Function:
 * author: limh
 * time:2017/7/28
 */
open class MultiItemTypeAdapter<T>(protected var mContext: Context, var datas: List<T>) : RecyclerView.Adapter<ViewHolder>() {


    protected var mItemViewDelegateManager: ItemViewDelegateManager<T>? = null
    protected var mOnItemClickListener: OnItemClickListener? = null


    init {
        this.datas = datas
        mItemViewDelegateManager = ItemViewDelegateManager()
    }

    override fun getItemViewType(position: Int): Int {
        if (!useItemViewDelegateManager()) return super.getItemViewType(position)
        return mItemViewDelegateManager!!.getItemViewType(datas[position], position)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemViewDelegate = mItemViewDelegateManager!!.getItemViewDelegate(viewType)
        val layoutId = itemViewDelegate.itemViewLayoutId
        val holder = ViewHolder.createViewHolder(mContext, parent, layoutId)
        onViewHolderCreated(holder, holder.getConvertView())
        setListener(parent, holder, viewType)
        return holder
    }

    fun onViewHolderCreated(holder: ViewHolder, itemView: View) {

    }

    fun convert(holder: ViewHolder, t: T) {
        mItemViewDelegateManager!!.convert(holder, t, holder.getAdapterPosition())
    }

    protected fun isEnabled(viewType: Int): Boolean {
        return true
    }


    protected fun setListener(parent: ViewGroup, viewHolder: ViewHolder, viewType: Int) {
        if (!isEnabled(viewType)) return
        viewHolder.run {
            getConvertView().setOnClickListener { v ->
                if (mOnItemClickListener != null) {
                    val position = viewHolder.getAdapterPosition()
                    mOnItemClickListener!!.onItemClick(v, viewHolder, position)
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        convert(holder, datas[position])
    }

    override fun getItemCount(): Int {
        val itemCount = datas.size
        return itemCount
    }

    fun addItemViewDelegate(itemViewDelegate: ItemViewDelegate<T>): MultiItemTypeAdapter<*> {
        mItemViewDelegateManager!!.addDelegate(itemViewDelegate)
        return this
    }

    fun addItemViewDelegate(viewType: Int, itemViewDelegate: ItemViewDelegate<T>): MultiItemTypeAdapter<*> {
        mItemViewDelegateManager!!.addDelegate(viewType, itemViewDelegate)
        return this
    }

    protected fun useItemViewDelegateManager(): Boolean {
        return mItemViewDelegateManager!!.itemViewDelegateCount > 0
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, holder: RecyclerView.ViewHolder, position: Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.mOnItemClickListener = onItemClickListener
    }
}